﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ESRP
{
    public partial class SetSpecialContFrm : FrmCard
    {
        public SetSpecialContFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();
        }

        private void SetSpecialContFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            conturiTableAdapter.Connection = con;
            conturiTableAdapter.Fill(eSRPDataSet.Conturi, ModuleInfo.CodUnitate);
            InchideConexiune(con);

            AddButton("Seteaza", global::ERPStudio.Properties.Resources.salvare1, SeteazaConturi, ToolStripItemAlignment.Right, true);
        }

        private void SeteazaConturi(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO [ConturiSpeciale] ([Cont],[CodUnitate],[Tipuri],[Descriere])", DeschideConexiune(workConnString));
            cmd.Transaction = cmd.Connection.BeginTransaction();
            for (Int16 i = 0; i < conturiSelectateBindingSource.Count; i++)
            {
                cmd.CommandText = "INSERT INTO [ConturiSpeciale] ([Cont],[CodUnitate],[Tipuri],[Descriere]) VALUES ('" +
                   ((CapitoleSelectate)conturiSelectateBindingSource.List[i]).Name.ToString() + "'," + ModuleInfo.CodUnitate.ToString() + ",'CCS','Cont de casa')";
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    cmd.Transaction.Rollback();
                    InchideConexiune(cmd.Connection);
                    return;
                }
            }
            cmd.Transaction.Commit();
            InchideConexiune(cmd.Connection);
            
        }

        private void addArt_Click(object sender, EventArgs e)
        {
            conturiSelectateBindingSource.Add(new CapitoleSelectate(contCB.Text,contCB.SelectedValue.ToString()));
            
        }

        private void delArt_Click(object sender, EventArgs e)
        {
            conturiSelectateBindingSource.RemoveCurrent();
        }
        
    }
}
